<template>
    <el-row type="flex" justify="center">
      
    <el-col :span="16" class="boxdiv">
    <h1 style="color:#fff;">登陆</h1>
      <el-form :model="form" ref="form" :rules="rules" >
        <el-form-item  prop="username" >
          <el-input type="text" placeholder="请输入用户名" v-model="form.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  prop="password" >
          <el-input type="password" placeholder="请输入密码" v-model="form.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-row :gutter="10">
            <el-col :span="12"><el-button type="primary" style="width:100%" @click="submitForm('form')">登录</el-button></el-col>
            <el-col :span="12"><el-button style="width:100%" @click="showregister">没有账号去注册</el-button></el-col>
          </el-row> 
        </el-form-item>
      </el-form>
    </el-col>
    </el-row>
</template>

<script>
export default {
  name: 'userlogin',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.H.ajax('/common/login/login', this.form, 'post', (data) => {
            // 登陆成功
            if (data.success) {
              // 赋值vuex和sessionstorage
              this.H.sendstore('userinfo', data.data)
              this.H.SL('userinfo', data.data)
              let localsetting = {
                backgroundtype: data.data.backgroundtype,
                backgroundimg: data.data.headimg ? data.data.headimg : 'static/bg.jpeg',
                diycolor: data.data.diycolor
              }
              this.H.sendstore('localsetting', localsetting)
              this.H.SL('localsetting', localsetting)
              // 设置首页的主题为自己（根据用户设置判断）
              if (Number(data.data.auto_setindex) === 1) {
                this.H.SL('showindexuid', data.data.id)
                this.H.sendstore('showindexuid', data.data.id)
              }
              // 提示登录成功
              this.H.success(data.info)
              // 跳转
              this.$store.state.next() || this.$router.push({path: '/user/usertopic'})
            } else {
              this.$message.error(data.info)
            }
          })
        } else {
          return false
        }
      })
    },
    showregister () {
      this.$router.push({path: '/userregister'})
    }
  },
  mounted: function () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.boxdiv{
  padding:10px 60px 40px;
  margin:0px auto;
  background-color: rgba(2, 1, 2, 0.32);
  border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,.15);
  border-color: transparent;
  margin-top:100px;
}
</style>
